//
//  VistorView.swift
//  weibo001
//
//  Created by ios on 15/11/4.
//  Copyright © 2015年 ios. All rights reserved.
//

import UIKit

class VistorView: UIView {

    //设置视图信息
    
    func setupInfo(imageName:String?,title:String) {
    
     messageLable.text = title
        
        
        guard let imageName = imageName else {
        
        startAnim()
        
        return
        }
        
        
        iconView.image = UIImage(named:imageName)
    
        homeIconView.hidden = true
        
        sendSubviewToBack(maskIconView)
    
    
    
    }
    
   //开启动画
    private func startAnim() {
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
    
    anim.removedOnCompletion = false
        
       iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
        
    }
    
    //MARK:懒加载空间
   //图标
    private lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩图片
    private lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 小房子
    private lazy var homeIconView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house") )
    // 消息文字
    private lazy var messageLable :UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    
    
    
//    
//    {
//        let label = UILabel()
//        
//        label.text = "关注一些人，回这里看看有什么惊喜"
//        label.font = UIFont.systemFontOfSize(14)
//        label.numberOfLines = 0
//        //文字水平居中
//        label.textAlignment = NSTextAlignment.Center
//        
//        return label
//        
//        }()
    //注册按钮
     lazy var registerButton: UIButton = UIButton(title: "注册", imageName: "common_button_white_disable", color: .orangeColor())
    
    
    
//    
//    {
//        
//        let button = UIButton()
//        button.setTitle("注册", forState: UIControlState.Normal)
//        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//        
//        
//        return button
//        }()
    
    //登录按钮
     lazy var loginButton: UIButton = UIButton(title: "登录", imageName: "common_button_white_disable", color: .darkGrayColor())
    
    
    
//    {
//        
//        let button = UIButton()
//        button.setTitle("登录", forState: UIControlState.Normal)
//        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
//        
//        return button
//        }()

    
}



//MARK :设置界面

extension VistorView {

    private func setupUI() {
 //1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
//2.自动布局
        for v in subviews {
        
        v.translatesAutoresizingMaskIntoConstraints = false
            
        }
        
        
     //图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
     //小房子
     addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
     
       //文字
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 36))
        
       //注册按钮
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 36))
        
        //登录按钮
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 36))
 
        
        
        
    //遮罩图片
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(-36)-[registButton]", options: [], metrics: nil, views: ["mask":maskIconView,"registButton":registerButton]))
        
        //设置背景颜色
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        
        
    }
}











